package com.hdm.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.hdm.bean.ProfileTO;
import com.hdm.delegate.SecurityUserDelegate;
import com.hdm.exception.ConnectionException;
import com.hdm.util.UtilConstants;

public class NewPasswordAction extends HttpServlet {



	/**
	 * 
	 */
	private static final long serialVersionUID = -2941564269120432640L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		RequestDispatcher rd=null;

		boolean flag=false;
		String path="";
		
		ProfileTO pro=new ProfileTO();
		
		Map map=request.getParameterMap();
	 try {
		BeanUtils.populate(pro, map);
	} catch (IllegalAccessException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (InvocationTargetException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		
	}
		
	try{		
		try{
		
		flag=new SecurityUserDelegate().forgetPass(pro);
			}
	    catch (ConnectionException ce) {
		throw new ServletException("server busy please try again");
	    }
	    
	   if(flag){
		   
		   request.setAttribute("status", UtilConstants._PASSWORD_SUCCESS);
		   
		   path=UtilConstants._USER_PASSWORD_CHANGE;
		   
		    }
	   else{
           request.setAttribute("status", UtilConstants._PASSWORD_FAILED);
		   
		   path=UtilConstants._STATUS;
		   }
		    	
		
		
	}
	catch (Exception e) {
		e.printStackTrace();
		
		request.setAttribute("status", "Enter  Password properly");
		     path=UtilConstants._STATUS;
		}
	
	rd=request.getRequestDispatcher(path);
	
	rd.forward(request, response);
	
		
	}

}
